// pages/index/store_admin/store_edit.js
Page({

  data: {    printtype:[],url:'',url1:'',urllist:[],radio:'',name:'',phone:'',beizhu:'',id:'',
  //要上传的图片路劲
   file:'',str:'',type:'',arr2:[],index2:0,arr3:[],radio3:'',phoneflag:false,radio4:2,address:'',lng:'',lat:'',last:''

  },

  onLoad: function (options) {
    
     this.setData({type:options.type})
    this.gettianjialist()
    if(options.type==3){
      this.getuserid()
    }

  },
  gettianjialist(){
    wx.request({
      url: 'https://zujieba.com/Api/storeadminstore/type.html',
      success:(res)=>{
       console.log(res);
    let printtype=res.data.data
    this.setData({printtype})
     }
    })
  },
  toggle(e){
      let list=this.data.printtype
      let index=e.currentTarget.dataset.index
      list[index].checked=!list[index].checked
      console.log(list)
      this.setData({
        printtype:list
      })
      
  },
  //上传照片
   upload(){
     
     wx.chooseImage({
       count:1,
       success:(res)=>{
         console.log(res)
       let url=  res.tempFiles[0].path
       wx.uploadFile({
        filePath: url,
        name: 'file',
        url: 'https://zujieba.com/index/upload/upload.html',
       success:(res1)=>{
         console.log(res1,1111);
         let data=JSON.parse(res1.data)
          this.setData({
            file:data.name
          })
         
       },
       fail:(res1)=>{
         console.log(res1,2);
         
       }
      })
       this.setData({
         
         url
       })
       }
     })
   },
   upload1(){
     let urllist=this.data.urllist
    wx.chooseImage({
      count:1,
      success:(res)=>{
        console.log(res)
      let url1=  res.tempFiles[0].path
      urllist.push(url1)
      
      //循环遍历
      let str=''
      for(var i=0;i<urllist.length;i++){
         wx.uploadFile({
           filePath: urllist[i],
           name: 'file',
           url: 'https://zujieba.com/index/upload/upload.html',
           success:(res1)=>{
             console.log('chenggong',res1);
             
               let data=JSON.parse(res1.data)
               if(str==''){str=data.name}else{str=str+','+data.name}
               
               this.setData({str})
               console.log(str);
           }
         })
      }
      
      
      this.setData({
        urllist
      })
      }
    })
  },
 
 
   del(){
     this.setData({
       url:''
     })
   },
   del1(e){
     let urllist=this.data.urllist
     urllist.splice(e.currentTarget.dataset.index,1)
    this.setData({
      urllist
    })
  },
  //选择是否配送
  toggle1(e){
     this.setData({
       radio:e.currentTarget.dataset.radio
     })
  },
  getdistance(e){
      this.setData({
        distance:e.detail.value
      })
  },
  getmoneyvalue(e){
    this.setData({
      money:e.detail.value
    })
  },
  change1(e){
    this.setData({
      name:e.detail.value
    })
  },
  change2(e){
     console.log(e.detail.value,'shiqujiaodiao');
     
    this.checkMobile1(e.detail.value)
  },
  change8(e){
    console.log(e.detail.value,'inputshijian');
    let phoneflag= this.checkMobile(e.detail.value)
    this.setData({
      phone:e.detail.value,
     phoneflag
    })
  },
  change3(e){
    console.log(e.detail.value);
    
    this.setData({
      beizhu:e.detail.value
    })
  },
  //添加店铺
  tianjia(){
   
    this.throttle(()=>{
      let mune=''
      this.data.printtype.forEach((item,index)=>{
          if(item.checked==true){
          /*  mune.push(String(item.id))*/
           mune=mune+","+item.id
            
          }
      })
      
      if(this.data.name==''||this.data.file==''||this.data.phone==''||this.data.radio==''||this.data.str==''||mune==""||this.data.beizhu==''||this.data.radio3==""||
      this.data.address==''||this.data.radio4==2
      )
      {
        return wx.showToast({
          title: '请完善信息',
          icon:'none'
        })
        if(this.data.radio==1){
            if(this.data.money==''||this.data.distance==''){
              return wx.showToast({
                title: '请完善信息',
                icon:'none'
              })
            }
        }
  
      }
      console.log(mune );
      console.log(this.data.file);
      
      wx.request({
        url: 'https://zujieba.com/Api/storeadminstore/add_store.html',
        data:{title:this.data.name,pic:this.data.file,uid:wx.getStorageSync('uid'),city:this.data.address,lat:this.data.lat,lng:this.data.lng,phone:this.data.phone,tp:this.data.radio,range:this.data.distance,remark:this.data.beizhu,package:this.data.radio3,express:this.data.radio4,dema:this.data.money,
      banner:this.data.str,mune},
      success:(res)=>{
        console.log(res);
        if(res.data.success==1){
          wx.navigateBack({
            complete: (res) => {},
          })
          wx.showToast({
            title:'添加成功',
            duration:2000,
            icon:'success'
          })
        }else{
          wx.showToast({
            title: res.data.tips,
            icon:'none'
          })
        }
        
      }
      })


    },1500)
  },
  //共享商添加店铺
  tianjia1(){
    console.log(111);
    
      this.throttle(()=>{

        if(this.data.arr3.length==0){return wx.showToast({
          title: '您还没有下级用户',
          icon:'none'
        })}
        if(this.data.name==''||this.data.file==''||this.data.phone==''||this.data.radio==''||this.data.str==''||mune==""||this.data.beizhu==''||this.data.radio3==""||this.data.address==''||this.data.radio4==2){return wx.showToast({
          title: '请完善信息',
          icon:'none'
        })
        if(this.data.radio==1){
          if(this.data.money==''||this.data.distance==''){
            return wx.showToast({
              title: '请完善信息',
              icon:'none'
            })
          }
      }
      }
       
        let mune=''
        this.data.printtype.forEach((item,index)=>{
            if(item.checked==true){
            /*  mune.push(String(item.id))*/
             mune=mune+","+item.id
              
            }
        })
        
        console.log(this.data.arr3[this.data.index2].id);
        
        
        wx.request({
          url: 'https://zujieba.com/Api/shareradminstore/add_store.html',
          data:{title:this.data.name,pic:this.data.file,
            pid:wx.getStorageSync('uid'),city:this.data.address,lat:this.data.lat,lng:this.data.lng,
            phone:this.data.phone,tp:this.data.radio,range:this.data.distance,
            uid:this.data.arr3[this.data.index2].id,
            banner:this.data.str,mune,dema:this.data.money,remark:this.data.beizhu,express:this.data.radio4,
          package:this.data.radio3},
        success:(res)=>{
          console.log(res);
          if(res.data.success==1){
            wx.navigateBack({
              complete: (res) => {},
            })
            wx.showToast({
              title:'添加成功',
              duration:2000,
              icon:'success'
            })
          }else{
            wx.showToast({
              title: res.data.tips,
              icon:'none'
            })
          }
          
        }
        })
      },1500)
  },
  //获取共享商用户列表id
  getuserid(){
    wx.request({
      url: 'https://zujieba.com/Api/shareradminstore/member.html',
      data:{pid:wx.getStorageSync('uid')},
      success:(res)=>{
        if(res.data.success==1){
          console.log(res);
         let arr2= res.data.data
         let arr=[]
         arr2.forEach((item)=>{
             arr.push(item.name)
         })

         this.setData({
           arr3:arr2,
           arr2:arr
         })
          
      }}
    })
  },
  bindPickerChange2(e){
      this.setData({
        index2:parseInt(e.detail.value),
        
      })
      
      

  },
  toggle3(e){
      this.setData({
        radio3:e.currentTarget.dataset.radio3
      })
  },
  checkMobile(e){
    
    if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(e))){
       
        
        return false;
    }
    else{
      return true
    }
},
checkMobile1(e){
    console.log('调用了');
    
  if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(e))){
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'none'
      })
      
      return false;
  }
  else{
    return true
  }
},
toggle4(e){
    this.setData({
      radio4:e.currentTarget.dataset.radio
    })
},
getlocation(){
    wx.chooseLocation({
      success:(res)=>{
         console.log(res);
        this.setData({address:res.address,lng:res.longitude,lat:res.latitude})
      },
      fail:(res)=>{
        console.log(res,22);
        
      }
      
    })
},
 //节流
 throttle:function(func,wait){
  console.log(func,wait);
  
  let last =this.data.last
  console.log(last);
  
  let current_time= +new Date()
  if(current_time-last>wait){
    console.log(1111);
      console.log(current_time,current_time-last);
      
      func()
      last=+new Date()
      this.setData({
        last
      })
  }
    
     
  
   
},
  
  
})